Program SelfMadeDB;
uses crt;
const v='0.2a'; {version}
type agent=record  {main class of records}
        name:string [15];
        surname:string [15];
        patronym:string [15];

        country: string [15];
        city: string [15];
        adress: string [50]; {Russian-type}

        rank: string [10];
        rel: string [5]; {reliability}
        dob: string [10] {xx.xx.xxxx}
        end;
var  path:string; f:file of agent; i,l:byte; a:agent;

function FILEXIST (name:string):boolean;
  var fm:byte;f:file;b:boolean;
                begin
                    fm := FileMode;
                    FileMode := 0;
                    assign (f, name);
                    {$I-} reset(f); {$I+}
                    b := IOResult = 0;
                    if b then close(f);
                    filexist := b;
                    FileMode := fm;
                                                  end;
procedure Input(var b:agent);
{Procedure of reading from keyboard agent-file to buffer variable}
  begin
    with b do
      begin
        Write('>Enter name: '); ReadLn(name);
        Write('>surname: '); ReadLn(surname);
        Write('>patronym: '); ReadLn(patronym);
        Write('>country (dislocation): '); ReadLn(country);
        Write('>city: '); ReadLn(city);
        Write('>adress in russian format: '); ReadLn(adress);
        Write('>rank: '); ReadLn(rank);
        Write('>reliability of agent: '); ReadLn(rel);
        Write('>Date of Birth (dd.mm.yyyy): '); ReadLn(dob);
      end;
  end;
Procedure Output (b:agent;r:byte);
  var mn,yn,dn,age,y,m,d:word; q:TDateTime;
  begin
   with b do
     begin
       WriteLn('[ ID ', r, ' ]');
       {calculating age ToDo}
       {GetDate(dn,mn,yn);} {WIN}
       {yn:= YearOf(q);
       mn:= MonthOf(q);
       dn:= DayOf(q);
       Val(Copy(dob,1,2),d);
       Val(Copy(dob,4,5),m);
       Val(Copy(dob,7,10),y);
       WriteLn('Now is ',yn, ' and he was borned in ',y);
       age:= yn - y;
       if not (((mn-m)>0) and ((dn-d)>0)) then Dec(age);    }
       WriteLn('>Name: ', surname, ' ', name, ' ', patronym);
       WriteLn('>Date of birth: ', dob);
       WriteLn('>Dislocation: ', country, ', ', city, ', ', adress);
       WriteLn('>Rank: ', rank);
       WriteLn('>Reliability: ', rel);
    end;
  end;


begin
Chdir('/home/dragon/selfmadedb/');
{FILE OPENING AND MODE SELECTION SECTION}
ClrScr; i:=0;
WriteLn('>SM Secure database v',v);
WriteLn('>Warning! It is unsecure connection!');
{Write('>: '); ReadLn(i); end;}
Writeln('>Enter path to datafile:');
Write('>:'); ReadLn(path);
ClrScr;
assign (f, path);    {assigning <namefile>, entered fom keyboard to vairable}

Write('>Enter the number of records you want to add: '); ReadLn(l);
if l <> 0 then begin
        {check file existence:}
        {$I-} if  FILEXIST (path) then WriteLn('File already exists!') else Rewrite(f); {$+}  if IOresult <> 0 then WriteLn('Error on rewrite');
        for i:=1 to l do begin
                WriteLn('<<<<<<< ',i, ' <<<<<<<');
                Input(a);
                write(f,a);
                        end;
        Close(f);
        ClrScr;
        end;
{starting reading file}
{$I-}
Reset(f);
{$I+}
if IOresult = 0 then
        while not (eof(f)) do begin
        read (f,a); Output(a,i);
                        end
                 else WriteLn('Error on end');
Close(f);
ReadKey;
end.
